package cn.wen233.basic.utils;

import cn.wen233.basic.consts.Constants;

/**
 * 名称 工具类
 *
 * @author wenei
 * @date 2021-10-11 10:54
 */
public class NamingUtils {

    /**
     * 将groupName和serviceName拼装成一个字符串
     * @param groupName 群组名称
     * @param serviceName 服务名称
     * @return 拼装后的字符串
     */
    public static String getGroupedName(String groupName, String serviceName) {
        if (StringUtils.isBlank(groupName)) {
            throw new IllegalArgumentException("Param 'groupName' is illegal, groupName is blank");
        }
        if (StringUtils.isBlank(serviceName)) {
            throw new IllegalArgumentException("Param 'serviceName' is illegal, serviceName is blank");
        }
        return groupName + Constants.SERVICE_INFO_SPLITER + serviceName;
    }

    /**
     * 从拼装字符串中获取群组名称
     * @param groupWithServiceName 拼装的字符串
     * @return 群组名称
     */
    public static String getGroupName(String groupWithServiceName) {
        if (StringUtils.isBlank(groupWithServiceName)) {
            return StringUtils.EMPTY;
        }
        if (!groupWithServiceName.contains(Constants.SERVICE_INFO_SPLITER)) {
            return Constants.DEFAULT_GROUP;
        }
        return groupWithServiceName.split(Constants.SERVICE_INFO_SPLITER)[0];
    }

    /**
     * 从拼装的字符串中获取服务名称
     * @param groupWithServiceName 拼装的字符串
     * @return 服务名称
     */
    public static String getServiceName(String groupWithServiceName) {
        if (StringUtils.isBlank(groupWithServiceName)) {
            return StringUtils.EMPTY;
        }
        if (!groupWithServiceName.contains(Constants.SERVICE_INFO_SPLITER)) {
            return groupWithServiceName;
        }
        return groupWithServiceName.split(Constants.SERVICE_INFO_SPLITER)[1];
    }
}
